package uit.hcm.views;

import java.util.ArrayList;
import java.util.HashMap;

import uit.bus.LoTrinhTuyenActivity;
import uit.bus.LocationCurrentActivity;
import uit.bus.MainActivity;
import uit.bus.R;
import uit.bus.ThongTinTuyen;
import uit.bus.TimKiem;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LazyAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.list_row, null);

		TextView title = (TextView) vi.findViewById(R.id.title); // title

		ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
							
		RelativeLayout list = (RelativeLayout) vi.findViewById(R.id.apps_row);
		
		HashMap<String, String> song = new HashMap<String, String>();
		song = data.get(position);

		// Setting all values in listview
		title.setText(song.get(MainActivity.KEY_TITLE));
		title.setTextSize(20);
		
		ObjViews view = MainActivity.listView.get(position);
		int resID = activity.getResources().getIdentifier(
				view.getImgUrl(), "drawable",
				"uit.bus");
		thumb_image.setImageResource(resID);
		//imageLoader.DisplayImage(song.get(MainActivity.KEY_STATIC),
				//thumb_image);

		list.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Option(position);
				//Toast.makeText(activity, Integer.toString(position), Toast.LENGTH_SHORT).show();
			}
		});

		return vi;
	}
	
	public void Option(int position){
		Intent intent;
		switch (position) {
		case 0:
			intent = new Intent(activity,LocationCurrentActivity.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 1:
			intent = new Intent(activity,ThongTinTuyen.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 2:
			intent = new Intent(activity,TimKiem.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 3:
			intent = new Intent(activity,TimKiem.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 4:
			intent = new Intent(activity,LoTrinhTuyenActivity.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 5:
			intent = new Intent(activity,TimKiem.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		case 6:
			intent = new Intent(activity,TimKiem.class);
			activity.startActivity(intent);
			activity.finish();
			break;
		}
	}
}